$(function() {
	//公共关闭窗口
	$('body').on('click','.g_window .g_bg,.g_window .x',function(){
		var _this=$(this).parents('.g_window');
		_this.removeClass('on');
		_this.find('.fd').removeClass('on');
	});
	
	//公共on
	$('body .g_li').click(function() {
		$(this).siblings().toggleClass('on', false);
		$(this).toggleClass('on', true)
	});
	//公共on 2
	$('body .g_click').click(function(){
		$(this).toggleClass('on')
	});
	
	
	
	//判断宽和高 谁大
	bi_da_xiao(name=$('.g_quan_zi_list .l_middle span img,.g_quan_zi_list .l_middle_2 span img'));
	
	//本地使用
	var pageUrl = "/Coterie/trendsList.html?ajax_get=true";
	var page_num=1;
	var total=1;
	//下拉加载
	var chong_fu=0;//阻止重复执行
	$(document).on('touchstart','.g_jia_zai',function(e){
		var touch = e.originalEvent.targetTouches[0];
		Cy = touch.pageY;//获取初始 Y坐标
	});
	$(document).on('touchmove','.g_jia_zai',function(e){
		var touch = e.originalEvent.targetTouches[0];
		Zy  = touch.pageY//获取最新 Y坐标
		if(Cy > (Zy+60) && !$(this).hasClass('off') && chong_fu==0){//判断下拉距离  同时当前box是否已经禁止加载了 (在想禁止的box里添加 class="off"即可)
			this_height = parseFloat($(this).height()); //当前的高
			this_scTop = parseInt($(this).scrollTop());  //当前已下拉位置
			this_scHeig = parseFloat($(this)[0].scrollHeight); //下拉上限
			if((this_height+this_scTop) >= (this_scHeig-10)){//判断是否已拉到底
				g_deng_dai_2('加载中');
				jia_zai($(this),false);//执行函数
			};
		};
		if($(this).hasClass('TopScroll')){
			if((Cy+60) < Zy && !$(this).hasClass('off') && chong_fu==0){
				if($(this).scrollTop() > 0) return;
				g_deng_dai_2('加载中');
				jia_zai($(this),true);//执行函数
			};
		};
	});
	function jia_zai(a,b){
		chong_fu=1;//必要！ 阻止重复执行
		page_num++;
		var ajaxUrl = pageUrl + '&page='+page_num;
		$.ajax({
			type:"GET",
			url:ajaxUrl,
			success:function(ret){
				setTimeout(function(){//模拟数据加载 延迟 
					if(b){
						$('#content li').eq(0).before(ret)
					}else{
						$('#content').append(ret);
					}
					g_deng_dai_off();
					if(page_num>total){
						a.toggleClass('off',true);
					}
					chong_fu=0;//必要！允许下拉
					if(typeof out_true!=='undefined'){
						out();
					}
				},800);
			},
		});
	}
	
	
});



//公共提交转圈圈
function g_deng_dai(e){
	html='<div id="g_deng_dai">'
			+'<div class="fd">'
				+'<p>'+e+'....</p>'
				+'<span><img src="img/g_img/deng_dai.jpg"></span>'
			+'</div>'
			+'<div class="bg"></div>'
		+'</div>'	
	$('body').append(html);		
	$('body').toggleClass('fixed',true);
};
//公共提交转圈圈2
function g_deng_dai_2(e){
	html='<div id="g_deng_dai_2">'
			+'<div class="fd">'
				+'<span><img src="img/g_img/deng_dai.jpg"></span>'
				+'<p>'+e+'....</p>'
			+'</div>'
		+'</div>'	
	$('body').append(html);		
	$('body').toggleClass('fixed',true);
};
function g_deng_dai_off(){
	$('body').removeClass('fixed');
	$('#g_deng_dai,#g_deng_dai_2').remove();
}
//初始化 让图片开始加载
g_deng_dai_2()
g_deng_dai()
g_deng_dai_off()

//公共二维码
$(document).on('click','#g_er_wei_ma .bg',function(){
	$('#g_er_wei_ma').removeClass('on');
});
function er_wei_ma(hide,name,img,logo){
	if(hide){a='<span>您的专属二维码</span>'}else{a=''}
	if(name){b=name}else{b='名字获取失败'};
	if(img){c=img}else{c='img/image/ewm.png'};
	if(logo){d=logo}else{d='img/g_img/logo_1.png'};
	html='<div id="g_er_wei_ma">'
			+'<div class="fd">'
				+'<div class="top">'
					+'<div class="img_logo"><img src="'+d+'"></div>'
					+'<div class="wrds">'+b+'</div>'
				+'</div>'
				+'<div class="tp"><img src="'+c+'"></div>'
				+a
				+'<p>邀请护士好友，微信扫描或识别二维码关注并登录<br />“160小护”公众号，您可获得30积分/位的积分奖励。</p>'
			+'</div>'
			+'<div class="bg"></div>'
		+'</div>';
	$('body').append(html);		
};



//居中列表
function shang_height(name){
	name.each(function(){
		$(this).css({'margin-top':'-'+$(this).height()/2+'px'});
	});
}

//把高设置成和宽一样
function shang_lihe(name,off){
	if(off==1){
		name.css({'height':name.width(),'line-height':name.width()+'px'});
	}else{
		name.css({'height':name.width()});
	};
	
};

//给banner宽
function banner(name){
	name.each(function(){
		$('.swiper-wrapper',this).css('width',$('.swiper-slide',this).length*100+'%');
	});
}
//判断宽和高 谁大
function bi_da_xiao(name){
	var timer = setInterval(function() {
		var e='';
		for(i in name){
			if(name[i].complete){
				e++
			};
			if(e==name.length){
				name.each(function(){
					if($(this).width()>$(this).height()){
						$(this).css('height','100%');
					}else{
						$(this).css('width','100%');
					};
				});	
				clearInterval(timer);
			};
		};
    },50);
};




///前端使用  后端请删除
var myapp=angular.module("myapp", []).controller("indexController", function($scope) {
  $scope.$on('ngRenderHeaderFinished', function (ngRepeatFinishedEvent) {
    // todo
    // console.log('header.html loaded.');
  });
  $scope.$on('ngRenderFooterFinished', function (ngRepeatFinishedEvent) {
    // todo
    // console.log('footer.html loaded.');
  });
  $scope.$on('ngRenderFloatbarFinished', function (ngRepeatFinishedEvent) {
    // todo
    // console.log('floatbar.html loaded.');
  });
});
myapp.directive('onFinishRenderHeaderFilters',function ($timeout){
  return{
    restrict: 'EA',
    replace: true,
    link: function(scope,element,attr) {
      $timeout(function(){
        scope.$emit('ngRenderHeaderFinished');
      });
    }
  };
});
myapp.directive('onFinishRenderFooterFilters',function ($timeout){
  return{
    restrict: 'EA',
    replace: true,
    link: function(scope,element,attr) {
      $timeout(function(){
        scope.$emit('ngRenderFooterFinished');
      });
    }
  };
});
myapp.directive('onFinishRenderFloatbarFilters',function ($timeout){
  return{
    restrict: 'EA',
    replace: true,
    link: function(scope,element,attr) {
      $timeout(function(){
        scope.$emit('ngRenderFloatbarFinished');
      });
    }
  };
});

